package com.liuzhilin.controller;

import com.liuzhilin.domain.Manager;
import com.liuzhilin.domain.User;
import com.liuzhilin.service.ManagerService;
import com.liuzhilin.service.UserService;
import com.liuzhilin.util.Response;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
/*
* 管理员信息查询
* */
@WebServlet("/managerInformation")
public class ManagerInformation extends HttpServlet{
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
            // 设置响应类型为 JSON
            Response.setCorsHeaders(response);
            response.setCharacterEncoding("UTF-8");
            // 设置响应内容类型为 JSON
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            // 从请求中获取用户名
            String username = request.getParameter("username");
            username = username.trim();
            // 查询用户信息
            String adminArea= ManagerService.managerInformationSelect(username);
            adminArea =adminArea+"管理员";
            System.out.println(adminArea);


            if (adminArea != null) {
                // 将用户信息转换为 JSON 格式
                String json = "{" +
                        "\"username\": \"" + username + "\"," +
                        "\"adminArea\": \"" + adminArea + "\"" +
                        "}";

                // 返回 JSON 响应
                out.print(json);
                System.out.println("username:"+username);
                response.setStatus(HttpServletResponse.SC_OK);
            } else {
                // 用户不存在，返回错误信息
                out.print("{\"message\": \"管理员不存在！\"}");
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            }
        }
        @Override
        public void doOptions(HttpServletRequest request, HttpServletResponse response){
            Response.setCorsHeaders(response);
            response.setStatus(HttpServletResponse.SC_OK);

        }

}

